package com.nullpointer.xchatfinal.object;

import java.util.Collection;

import android.content.Context;

import com.nullpointer.xchatfinal.object.MessageContent.MsgType;
import com.nullpointer.xchatfinal.utils.DataManager;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class PrivateMessageContent {
	private MessageContent messageContent;
	private String targetUserID;
	private String targetUserName;

	public PrivateMessageContent() {
		super();
	}

	public PrivateMessageContent(Context context, String message,
			MsgType msgType, TargetChat targetChat) {
		super();
		User currentUser = DataManager.getInstance(context).getCurrentUser();
		MessageContent messageContent = new MessageContent(currentUser.getUserID());
		messageContent.setContent(message);
		messageContent.setMsgType(msgType);
		messageContent.setName(currentUser.getName());
		messageContent.setAvatarID(currentUser.getUserInfo().getAvatarID());
		
		this.setMessageContent(messageContent);
		this.setTargetUserID(targetChat.getFromID());
		this.setTargetUserName(targetChat.getName());
	}

	public PrivateMessageContent(Context context, String message,
			MsgType msgType, String targetID) {
		super();
		User currentUser = DataManager.getInstance(context).getCurrentUser();
		MessageContent messageContent = new MessageContent(currentUser.getUserID());
		messageContent.setContent(message);
		messageContent.setMsgType(msgType);
		messageContent.setName(currentUser.getName());
		messageContent.setAvatarID(currentUser.getUserInfo().getAvatarID());
		this.setMessageContent(messageContent);
		this.setTargetUserID(targetID);
		this.setTargetUserName("");
	}

	public MessageContent getMessageContent() {
		return messageContent;
	}

	public void setMessageContent(MessageContent messageContent) {
		this.messageContent = messageContent;
	}

	public String getTargetUserID() {
		return targetUserID;
	}

	public void setTargetUserID(String targetUserID) {
		this.targetUserID = targetUserID;
	}

	public String getTargetUserName() {
		return targetUserName;
	}

	public void setTargetUserName(String targetUserName) {
		this.targetUserName = targetUserName;
	}

	public String toJson() {
		return new JSONSerializer().exclude("*.class").deepSerialize(this);
	}

	public static PrivateMessageContent fromJsonToGroupMessageContent(
			String json) {
		try {
			PrivateMessageContent groupMessageContent = new JSONDeserializer<PrivateMessageContent>()
					.use(null, PrivateMessageContent.class).deserialize(json);
			return groupMessageContent;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String toJsonArray(
			Collection<PrivateMessageContent> collection) {
		return new JSONSerializer().exclude("*.class")
				.deepSerialize(collection);
	}

}
